package sets.collection;

import sets.collection.domain.Student;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionDemo02 {
    public static void main(String[] args) {
        Collection<Student> c = new ArrayList<>();
        c.add(new Student("张三",21));
        c.add(new Student("lisa",20));
        c.add(new Student("JACK",25));

        //1.迭代器遍历
        Iterator<Student> it = c.iterator();
        while(it.hasNext()){
            //next执行一次，指针就++
            Student stu = it.next();
            System.out.println(stu.getAge()+"---"+stu.getName());
        }

        //2.增强for循环(快捷键:c.for)
        //底层还是使用迭代器
        for(Student stu:c){
            System.out.println(stu);
        }

        //3.foreach方法
        //参数管你要接口，传进去实现类对象，使用匿名内部类
//        c.forEach(new Consumer<Student>() {
//            @Override
//            public void accept(Student student) {
//                System.out.println(student);
//            }
//        });

        //使用lambda
        c.forEach(student -> System.out.println(student));
    }
}
